﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Comp_GeoData;

namespace Comp_Geo
{
    public class GameObjectManager : DrawableGameComponent
    {
        #region Members

        SpriteBatch spriteBatch;

        private List<GameObject> gameObjects;

        private string gameMap = "Map1";
        private Map map;
        private List<LineSegment> lineSegments;
        private Texture2D dotTexture;
        private Color color;

        private Kitten kitten;
        private Texture2D kittenTexture;
        private Monster monster;
        private Texture2D monsterTexture;

        private Triangulator triangulator;
        private Graph graph;

        #endregion

        #region Paramaters

        public List<GameObject> GameObjects { get { return gameObjects; } set { gameObjects = value; } }

        #endregion

        #region Initilization

        public GameObjectManager(Game game)
            : base(game)
        {
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            map = Game.Content.Load<Map>(gameMap);
            kittenTexture = Game.Content.Load<Texture2D>("kitten");
            monsterTexture = Game.Content.Load<Texture2D>("monster");
            dotTexture = Game.Content.Load<Texture2D>("dot");

            lineSegments = map.LineSegments;
            kitten = new Kitten(kittenTexture, map.KittenStartPosition);
            monster = new Monster(monsterTexture, map.MonsterStartPosition);
            triangulator = new Triangulator(lineSegments, monster.CurrPosition, kitten.CurrPosition);
            graph = triangulator.Triangulate();
            monster.AStar(monster.CurrPosition, kitten.CurrPosition, graph);
   
            color = Color.Red;

            base.LoadContent();
        }

        #endregion

        #region Draw and Update

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            map.DrawMap(gameTime, spriteBatch, dotTexture);
            kitten.Draw(gameTime, spriteBatch);
            monster.Draw(gameTime, spriteBatch);
            //graph.DrawGraph(gameTime, spriteBatch, dotTexture);

            spriteBatch.End();
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            monster.Update(gameTime);
            base.Update(gameTime);
        }

        #endregion
    }
}
